//
//  TSAccountSectionHeaderView.swift
//  TSProfitProject
//
//  Created by TMS on 2019/11/6.
//  Copyright © 2019 TMS. All rights reserved.
//

import UIKit

protocol TSAccountSectionHeaderViewDelegate : NSObjectProtocol {
    func didClickItem(title : String)
}

class TSAccountSectionHeaderView: UIView {

    weak var delegate: TSAccountSectionHeaderViewDelegate?
    var cashItemArray : [TSAccountHeaderCashItem] = []
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.white
        configViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func tapAction(action:UITapGestureRecognizer) -> Void {

        let handleItem = action.view as! TSAccountHeaderCashHandleItem
        
        if self.delegate != nil {
            self.delegate?.didClickItem(title: handleItem.title!)
        }

    }
    
    func refreshSectionHeaderInfo() {
        for cashItem in cashItemArray {
            if cashItem.titleLabel.text == "中心钱包" {
                cashItem.setupCashItem(value: TSLoginManager.shared._usermodel?.zhongxin ?? 0)
            } else if cashItem.titleLabel.text == "金币钱包" {
                cashItem.setupCashItem(value: TSLoginManager.shared._usermodel?.jinbi ?? 0)
            } else if cashItem.titleLabel.text == "盈利钱包" {
                cashItem.setupCashItem(value: TSLoginManager.shared._usermodel?.yingli ?? 0)
            } else {
                cashItem.setupCashItem(value: TSLoginManager.shared._usermodel?.yongjin ?? 0)
            }
        }
    }
    
    func configViews() {
        
        let itemContainer = UIView.init()
        itemContainer.backgroundColor = WHITE_COLOR
        self.addSubview(itemContainer)
        itemContainer.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview().inset(12)
            make.top.equalTo(15)
            make.height.equalTo(140)
        }
        
        let centerItem = TSAccountHeaderCashItem.init("中心钱包")
        let coinItem = TSAccountHeaderCashItem.init("金币钱包")
        let gainItem = TSAccountHeaderCashItem.init("盈利钱包")
        let profitItem = TSAccountHeaderCashItem.init("收益钱包")
        
        cashItemArray.append(gainItem)
        cashItemArray.append(profitItem)
        cashItemArray.append(centerItem)
        cashItemArray.append(coinItem)
        
        var itemX : CGFloat = 0
        var itemY : CGFloat = 0
        let itemW = (SCREEN_WIDTH - 2*12) * 0.5
        let itemH : CGFloat = 70.0
        for index in 0..<cashItemArray.count {
            let item = cashItemArray[index]
            itemX = index % 2 == 0 ? 0 : itemW
            itemY = index / 2 >= 1 ? 0 : itemH
            item.frame = CGRect(x: itemX, y: itemY, width: itemW, height: itemH)
            itemContainer.addSubview(item)
        }
        
        let horizonLine = UIView.init()
        horizonLine.backgroundColor = UIColor.hexadecimalColor(hexadecimal: "#E9EDF5")
        itemContainer.addSubview(horizonLine)
        horizonLine.snp.makeConstraints { (make) in
            make.left.right.centerY.equalToSuperview()
            make.height.equalTo(1)
        }
        
        let verticalLine = UIView.init()
        verticalLine.backgroundColor = UIColor.hexadecimalColor(hexadecimal: "#E9EDF5")
        itemContainer.addSubview(verticalLine)
        verticalLine.snp.makeConstraints { (make) in
            make.centerX.top.bottom.equalToSuperview()
            make.width.equalTo(1)
        }
        
        let handleItemContainer = UIView.init()
        handleItemContainer.backgroundColor = WHITE_COLOR
        self.addSubview(handleItemContainer)
        handleItemContainer.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(verticalLine.snp_bottom).offset(18)
            make.height.equalTo(80)
        }

        var handleArray : [TSAccountHeaderCashHandleItem] = []
        let titleArray = ["充值", "提现", "额度转换", "公告"]
        for index in 0..<4 {
            let handleItem = TSAccountHeaderCashHandleItem.init("main_item\(index)", title: titleArray[index])
            handleItem.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(tapAction(action:))))
            handleItemContainer.addSubview(handleItem)
            handleArray.append(handleItem)
        }

        handleArray.snp.distributeViewsAlong(axisType: .horizontal, fixedItemLength: 50, leadSpacing: SCREEN_WIDTH*0.11, tailSpacing: SCREEN_WIDTH*0.11)
        handleArray.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.height.equalTo(60)
        }
        
        let line = UIView.init()
        line.backgroundColor = UIColor.hexadecimalColor(hexadecimal: "#F5F5F5")
        self.addSubview(line)
        line.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
    }

}

class TSAccountHeaderCashItem: UIView {

    var titleLabel : UILabel!
    var valueLabel : UILabel!
    
    convenience init(_ title:String) {
        self.init()
        self.configViews(title: title)
    }
    
    func setupCashItem(value:CGFloat) {
        valueLabel.text = String.init(format: "￥%.2f", value)
    }
    
    func configViews(title:String) {
        
        titleLabel = UILabel.init()
        titleLabel.text = title
        titleLabel.textColor = UIColor.hexadecimalColor(hexadecimal: "#292929")
        titleLabel.font = UIFont.systemFont(ofSize: 17, weight: .bold)
        self.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(8)
        }
        
        valueLabel = UILabel.init()
        valueLabel.text = "￥0.00"
        valueLabel.adjustsFontSizeToFitWidth = true
        valueLabel.minimumScaleFactor = 0.6
        valueLabel.textColor = UIColor.hexadecimalColor(hexadecimal: "#A5ABB7")
        valueLabel.font = UIFont.systemFont(ofSize: 12, weight: .bold)
        valueLabel.textAlignment = .center
        self.addSubview(valueLabel)
        valueLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(titleLabel.snp_bottom).offset(12)
            make.left.right.equalToSuperview().inset(5)
        }
    }
}

class TSAccountHeaderCashHandleItem: UIView {
    
    var title : String?
    
    convenience init(_ imgName:String, title:String) {
        self.init()
        self.configViews(imgName: imgName, title: title)
    }
    
    func configViews(imgName:String, title:String)  {
        
        let icon = UIImageView.init(image: UIImage(named: imgName))
        self.addSubview(icon)
        icon.snp.makeConstraints { (make) in
            make.centerX.top.equalToSuperview()
            make.size.equalTo(CGSize(width: 31, height: 33))
        }
        
        self.title = title
        let titleLabel = UILabel.init()
        titleLabel.text = title
        titleLabel.textColor = UIColor.black
        titleLabel.font = UIFont.systemFont(ofSize: 12, weight: .medium)
        self.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(icon.snp_bottom).offset(10)
        }
    }
}
